﻿using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace BioinformaticAlgoritms2014
{
    public class RNASequence : SequenceBase
    {
        public RNASequence(string sequence) : base(sequence)
        {
        }

        public RNASequence(char[] seqChars) : this(new string(seqChars))
        {
            
        }

        public static RNASequence LoadFromDnaString( string dnaString)
        {
            return new RNASequence(dnaString.ToUpper().Select(a => a == 'T' ? 'U' : a).ToArray());
        }

        public string TranslateToAminoAcidSeq()
        {
            var codons = GetCodonTable();
            return string.Join("",GetCodonsSequence().Select(a => codons[a]));
        }

        private Dictionary<string,string> GetCodonTable()
        {
          var lines = File.ReadAllLines(Tools.GetFullPath(@"Data\RNA_codon_table_1.txt"));
          return  lines.Select(a => a.Split(' ')).Where(a => a.Length > 1).ToDictionary(a => a[0], a => a[1]);

        }

        public string[] GetCodonsSequence()
        {
            var result= new List<string>();
            for (int i = 0; i <= Sequence.Length-3; i=i+3)
            {
                result.Add(Sequence.Substring(i,3));
            }
            return result.ToArray();
        }
    }
}
